package week02;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import static java.lang.System.out;

import java.io.IOException;

public class TrafficMapper extends Mapper<LongWritable, Text, Text, TrafficRecord> {

    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, TrafficRecord>.Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] ary = line.split("\t",-1);
        if (ary.length > 10){
            String phoneNum = ary[1];
            String bytesUp = ary[8];
            String bytesDown = ary[9];
            try {
                TrafficRecord rec = new TrafficRecord(bytesUp,bytesDown);
                Text keyOut = new Text(phoneNum);
                context.write(keyOut,rec);
            }catch (NumberFormatException re){
                out.printf("Found Number Format Exception, at KEY:%d, and phoneNumber:%s, bytesUp:%s, bytesDown:%s",key.get(),phoneNum,bytesUp,bytesDown);
                out.println("");
            }
        }else {
            out.printf("Found Column Error, at KEY:%d, the column count is:%d" , key.get(),ary.length );
        }
    }

}
